<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        function Calculator() {
            this.methods = {
                '+': (a, b) => a + b,
                '-': (a, b) => a - b
            };
            this.calculate = function(str) {
                // `"1 + 2"`
                let arr = str.split(' ');
                console.log(arr);
                // if (arr[1] === '+') {
                //     return Number(arr[0]) + Number(arr[2]);
                // } else if (arr[1] === '-') {
                //     return Number(arr[0]) - Number(arr[2]);
                // }
                // // 然后这边计算的时候，可以知道如何计算
                // if (arr[1] === name) {
                //     return func(arr[0], arr[2]);
                // }
                if (this.methods[arr[1]]) {
                    return this.methods[arr[1]](Number(arr[0]), Number(arr[2]));
                }
            }

            this.addMethod = function(name, func) {
                this.methods[name] = func;
                console.log(this.methods);
            }
        }

        let calc = new Calculator();
        
        console.log(calc.calculate("3 + 7"));
    </script>
</body>
</html>